package com.xdog.web3.controller.transactionhistory;

import com.alibaba.fastjson.JSONObject;
import com.xdog.common.core.domain.R;
import com.xdog.web3.domain.transactionhistory.TransactionDetailByTxHashRequest;
import com.xdog.web3.domain.transactionhistory.TransactionsByAddressRequest;
import com.xdog.web3.exception.Web3ApiException;
import com.xdog.web3.service.ITransactionHistoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@Tag(name = "交易历史", description = "交易历史相关接口")
@RestController
@RequestMapping("/web3/transaction-history")
public class TransactionHistoryController {

    @Resource
    private ITransactionHistoryService transactionHistoryService;

    /**
     * 获取地址维度下的6个月内的交易历史，按时间倒序排列
     */
    @Operation(summary = "获取交易历史", description = "查询地址维度下的6个月内的交易历史，按时间倒序排列")
    @GetMapping("/transactions-by-address")
    public R<JSONObject> getTransactionsByAddress(TransactionsByAddressRequest request) {
        try {
            JSONObject result = transactionHistoryService.getTransactionsByAddress(request);
            return R.ok(result);
        } catch (Web3ApiException e) {
            return R.fail(e.getHttpStatus(), "API错误: " + e.getMessage());
        } catch (Exception e) {
            return R.fail("获取交易历史失败: " + e.getMessage());
        }
    }

    /**
     * 根据 txHash 查询6个月内的某个交易的详情
     */
    @Operation(summary = "获取特定交易", description = "根据 txHash 查询6个月内的某个交易的详情")
    @GetMapping("/transaction-detail-by-txhash")
    public R<JSONObject> getTransactionDetailByTxHash(TransactionDetailByTxHashRequest request) {
        try {
            JSONObject result = transactionHistoryService.getTransactionDetailByTxHash(request);
            return R.ok(result);
        } catch (Web3ApiException e) {
            return R.fail(e.getHttpStatus(), "API错误: " + e.getMessage());
        } catch (Exception e) {
            return R.fail("获取特定交易失败: " + e.getMessage());
        }
    }
}
